//
//  CallController.m
//  EncryPhone
//
//  Created by zhibao on 2023/6/16.
//

#import "MeContactUsController.h"

@interface MeContactUsController (){
    NSDictionary *dic;
    UIImage *codeImg;
}

@end

@implementation MeContactUsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = LocalizedString(@"联系我们");
    self.view.backgroundColor = MyColor(@"#EDEEF2");
    self.gk_navBackgroundColor = MyColor(@"#EDEEF2");
    // iOS 获取设备当前语言和地区的代码
    NSString *currentLanguageRegion = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];

    DLog(@"currentLanguageRegion==%@",currentLanguageRegion);
    __weak typeof(self) weakSelf = self;
    [[HttpHelper sharedInstance] sendGet:@"api/contact" parameters:@{} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        DLog(@"payfinish===%@",responseObject.data);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        self->dic = responseObject.data[@"data"];
        if (responseObject.code == 1) {
            if([currentLanguageRegion containsString:@"zh-Hans"]){
                [strongSelf initView];
            }else{
                [strongSelf initOutView];
            }
        }
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
    }];
}

-(void)initOutView{
    
    UIImageView *topImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_about"]];
    [self.view addSubview:topImgView];
    [topImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(120 - 64 + NAVIH);
    }];
    UILabel *ctip1label = [[UILabel alloc]initWithFrame:CGRectMake(15, 374 - 64 + NAVIH, MY_Screen_W - 30, 18)];
    ctip1label.text = LocalizedString(@"客服邮箱");
    ctip1label.font = MY_SCALE_RegularFONT(13);
    ctip1label.textColor = MyColor(@"999999");
    [self.view addSubview:ctip1label];
    
    UILabel *ctip2label = [[UILabel alloc]initWithFrame:CGRectMake(15, ctip1label.bottom + 3, MY_Screen_W - 30, 18)];
    ctip2label.text = dic[@"email"];
    ctip2label.font = MY_SCALE_RegularFONT(13);
    ctip2label.textColor = UIColor.blackColor;
    [self.view addSubview:ctip2label];
    
    UILabel *ctip3label = [[UILabel alloc]initWithFrame:CGRectMake(15, ctip2label.bottom + 41, MY_Screen_W - 30, 60)];
    ctip3label.text = LocalizedString(@"感谢反馈，我们将努力做的更好！");
    ctip3label.font = MY_SCALE_RegularFONT(13);
    ctip3label.numberOfLines = 0;
    ctip3label.lineBreakMode = NSLineBreakByWordWrapping;
    ctip3label.textColor = UIColor.blackColor;
    [self.view addSubview:ctip3label];
    
//    UILabel *ctip4label = [[UILabel alloc]initWithFrame:CGRectMake(15, ctip3label.bottom + 3, MY_Screen_W - 30, 18)];
//    ctip4label.text = dic[@"wx_name"];
//    ctip4label.font = MY_SCALE_RegularFONT(13);
//    ctip4label.textColor = MyColor(@"ffffff");
//    [self.view addSubview:ctip4label];
    
//    UILabel *ctip5label = [[UILabel alloc]initWithFrame:CGRectMake(15, ctip4label.bottom + 35, MY_Screen_W - 30, 100)];
//    ctip5label.numberOfLines = 0;
//    ctip5label.lineBreakMode = NSLineBreakByWordWrapping;
//    ctip5label.qmui_lineHeight = 20;
//    ctip5label.text = dic[@"wx_service_msg"];
//    ctip5label.font = MY_SCALE_RegularFONT(13);
//    ctip5label.textColor = UIColor.whiteColor;
//    [self.view addSubview:ctip5label];
}

-(void)initView{
    UIView *contentView = [UIView new];
    contentView.backgroundColor = MyColor(@"#ffffff");
    contentView.layer.cornerRadius = 10;
    [self.view addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(121 - 64 + NAVIH);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.equalTo(self.view).offset(-30 - SafeBottomMargin);
    }];
    
    UIImageView *headImgView = [UIImageView new];
    headImgView.layer.masksToBounds = YES;
    headImgView.layer.cornerRadius = 36;
    [headImgView yy_setImageWithURL:[NSURL URLWithString:dic[@"wx_portrait_url"]] options:YYWebImageOptionProgressive];
    [self.view addSubview:headImgView];
    [headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView);
        make.centerY.equalTo(contentView.mas_top);
        make.width.height.mas_equalTo(72);
    }];
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.font = MY_SCALE_MediumFONT(16);
    nameLabel.textColor = UIColor.blackColor;
    nameLabel.text = dic[@"wx_name"];
    [contentView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView);
        make.top.mas_equalTo(42);
    }];
    
    UIView *maBgView = [UIView new];
    maBgView.backgroundColor = UIColor.whiteColor;
    maBgView.layer.cornerRadius = 18;
    [contentView addSubview:maBgView];
    [maBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView);
        make.width.height.mas_equalTo(191);
        make.top.equalTo(nameLabel.mas_bottom).offset(34);
    }];
    
    UIImageView *maImgView = [UIImageView new];
    [maImgView yy_setImageWithURL:[NSURL URLWithString:dic[@"wx_qrcode_url"]] placeholder:nil options:YYWebImageOptionProgressive completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        self->codeImg = image;
    }];
    [maBgView addSubview:maImgView];
    [maImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(maBgView);
        make.width.height.mas_equalTo(163);
    }];
    
    UILabel *tip1Label = [UILabel new];
    tip1Label.font = MY_SCALE_RegularFONT(12);
    tip1Label.textColor = MyColor(@"#77818C");
    tip1Label.textAlignment = NSTextAlignmentCenter;
    tip1Label.numberOfLines = 0;
    tip1Label.qmui_lineHeight = 20;
    tip1Label.lineBreakMode = NSLineBreakByWordWrapping;
    tip1Label.text = LocalizedString(@"保存二维码至相册，微信扫一扫，即可添加客服小助手微信，获得一对一专属咨询服务。");
    [contentView addSubview:tip1Label];
    [tip1Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(29);
        make.right.mas_equalTo(-29);
        make.top.equalTo(maBgView.mas_bottom).offset(20);
    }];
    
    UILabel *tip2Label = [UILabel new];
    tip2Label.font = MY_SCALE_SemiboldFONT(12);
    tip2Label.textColor = MyColor(@"#666666");
    tip2Label.numberOfLines = 0;
    tip2Label.qmui_lineHeight = 20;
    tip2Label.lineBreakMode = NSLineBreakByWordWrapping;
    tip2Label.text = LocalizedString(@"客服在线时间：\n工作日：10:00-18:00");
    [contentView addSubview:tip2Label];
    [tip2Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(29);
        make.right.mas_equalTo(-29);
        make.top.equalTo(tip1Label.mas_bottom).offset(30);
    }];
    
    UILabel *tip3Label = [UILabel new];
    tip3Label.font = MY_SCALE_RegularFONT(12);
    tip3Label.textColor = MyColor(@"999999");
    tip3Label.numberOfLines = 0;
    tip3Label.qmui_lineHeight = 20;
    tip3Label.lineBreakMode = NSLineBreakByWordWrapping;
    tip3Label.text = LocalizedString(@"其他时间请留言，客服将在上线的第一时间答复您。");
    [contentView addSubview:tip3Label];
    [tip3Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(29);
        make.right.mas_equalTo(-29);
        make.top.equalTo(tip2Label.mas_bottom).offset(5);
    }];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setTitle:LocalizedString(@"保存至相册") forState:UIControlStateNormal];
    saveBtn.titleLabel.font = MY_SCALE_RegularFONT(14);
    saveBtn.layer.cornerRadius = 23;
    saveBtn.backgroundColor = MyColor(@"#027AFF");
    [saveBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [contentView addSubview:saveBtn];
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(nameLabel);
        make.top.equalTo(tip3Label.mas_bottom).offset(28);
        make.width.mas_equalTo(180);
        make.height.mas_equalTo(46);
    }];
}

-(void)save{
    if(codeImg){
        UIImageWriteToSavedPhotosAlbum(codeImg, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }else{
        [QMUITips showError:LocalizedString(@"图片下载中...")];
    }
}

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo{


    if(error != NULL){
        [QMUITips showError:LocalizedString(@"保存图片失败")];
    }else{
        [QMUITips showSucceed:LocalizedString(@"保存图片成功")] ;
    }
}

@end
